package shop.ynubeggar.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import shop.ynubeggar.model.dataobject.CategoryDO;
import shop.ynubeggar.model.dataobject.ProductDO;
import shop.ynubeggar.model.dataobject.ProductOrderDO;
import shop.ynubeggar.model.dataobject.ProductOrderItemDO;
import shop.ynubeggar.service.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "统计")
@RestController
@RequestMapping("api/admin/statistic/v1")
public class AdminStatisticController {

    @Resource
    AdminStatisticService adminStatisticService;
    @Resource
    AdminProductService adminProductService;
    @Resource
    AdminOrderService adminOrderService;
    @Resource
    AdminCategoryService adminCategoryService;
    @Resource
    ProductOrderItemService productOrderItemService;

    @ApiOperation(value = "商品分类统计")
    @GetMapping("category")
//    类别名，数量
    public Map<String, Integer> statisticCategory() {
//        产品id-产品类别id
        Map<Long, Long> map = adminProductService.page(new Page<ProductDO>(1, 9999)).getRecords()
                .stream().collect(Collectors.toMap(ProductDO::getId, ProductDO::getCategory));
//        订单列表
        List<Long> orders = adminOrderService.list(new QueryWrapper<ProductOrderDO>().in("del", 0, 1))
                .stream().map(ProductOrderDO::getId).collect(Collectors.toList());
//        子订单列表，有产品id
        List<ProductOrderItemDO> productOrderItemDOList = productOrderItemService.list(new QueryWrapper<ProductOrderItemDO>().in("product_order_id", orders));
//        类别id-类别名
        Map<Long, String> map1 = adminCategoryService.listByIds(map.values()).stream().collect(Collectors.toMap(CategoryDO::getId, CategoryDO::getName));

        Map<String, Integer> result = new HashMap<>();
        for (ProductOrderItemDO productOrderItemDO : productOrderItemDOList) {
            result.put(map1.get(map.get(productOrderItemDO.getProductId())),
                    result.getOrDefault(map1.get(map.get(productOrderItemDO.getProductId())), 0) + 1);
        }
        return result;
    }

    @ApiOperation(value = "商品单项统计")
    @GetMapping("items")
//    类别名，数量
    public Map<String, Integer> statisticItems() {
//        订单列表
        List<Long> orders = adminOrderService.list(new QueryWrapper<ProductOrderDO>().in("del", 0, 1))
                .stream().map(ProductOrderDO::getId).collect(Collectors.toList());
//        子订单列表，有产品id
        List<ProductOrderItemDO> productOrderItemDOList = productOrderItemService.list(new QueryWrapper<ProductOrderItemDO>().in("product_order_id", orders));

        Map<String, Integer> result = new HashMap<>();
        for (ProductOrderItemDO productOrderItemDO : productOrderItemDOList) {
            result.put(productOrderItemDO.getProductName(),
                    result.getOrDefault(productOrderItemDO.getProductName(), 0) + 1);
        }
        return result;
    }
}
